package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageServie;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuManageController {
    @Autowired
    private SpuManageService spuManageService;
    @Autowired
    private SpuImageServie spuImageServie;

    /**
     * 获取分页SPU信息
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuByPage(@PathVariable Long page,@PathVariable Long limit,@RequestParam Long category3Id) {
        Page<SpuInfo> pageInfo = new Page<>(page, limit);
        IPage<SpuInfo> pageModel = spuManageService.getSpuByPage(category3Id,pageInfo);
        return Result.ok(pageModel);
    }


    /**
     * 保存spuInfo
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuId 获取spuImage 集合
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId) {
        LambdaQueryWrapper<SpuImage> spuImageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuImageLambdaQueryWrapper.eq(SpuImage::getSpuId, spuId).orderByDesc(SpuImage::getId);
        List<SpuImage> spuImageList = spuImageServie.list(spuImageLambdaQueryWrapper);
        return Result.ok(spuImageList);
    }

    /**
     * 根据spuId 查询销售属性
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList= this.spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 根据getSpuInfo获取SPUInfo详细信息
     * @param spuId
     * @return
     */
    @GetMapping("/getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId) {
        SpuInfo spuInfo = spuManageService.getSpuInfo(spuId);
        return Result.ok(spuInfo);
    }

    /**
     * 修改SpuInfo信息
     * @param spuInfo
     * @return
     */
    @PostMapping("/updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

}
